package com.atguigu.gmall.index.controller;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.service.IndexService;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class IndexController {
    @Autowired
    IndexService indexService;

    @GetMapping("/**")
    public String index(Model model, @RequestHeader(value = "userId",required = false)Long userId){
        //查询一级分类
        System.out.println("成功获取到用户的id"+userId);
      List<CategoryEntity> categoryEntities =  indexService.queryLvl1Categories();
      model.addAttribute("categories",categoryEntities);
        return "index";
    }
    @ResponseBody
    @GetMapping("index/cates/{pid}")
    public ResponseVo<List<CategoryEntity>> queryLvl2CategoriesWithSub(@PathVariable Long pid){
        List<CategoryEntity> categoryEntities =  indexService.queryLvl2CategoriesWithSub(pid);
        return ResponseVo.ok(categoryEntities);
    }

    @GetMapping("index/test")
    @ResponseBody
    public ResponseVo<Object> pusherTest(){
        try {
            indexService.pusherTest();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ResponseVo.ok();
    }

}
